package com.lc.stock.module.filtrate.filter.impl;

import cn.hutool.core.collection.CollUtil;
import com.lc.stock.common.Constant;
import com.lc.stock.module.filtrate.filter.FiltrateStockFilter;
import com.lc.stock.module.floatingstockholder.dao.FloatingStockholderDao;
import com.lc.stock.module.floatingstockholder.domain.FloatingStockholder;
import com.lc.stock.module.floatingstockholder.domain.FloatingStockholderDetails;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author lichen
 * @description 筛选出 公司前十大流通股至少有n个机构存在
 * @menu
 * @date 2023/2/6 14:31
 */
@Slf4j
@Component
public class ExistOrgFilter implements FiltrateStockFilter {

    @Value("${coustomfilter.atLeastExistOrg}")
    private int atLeastExistOrg;

    @Autowired
    private FloatingStockholderDao floatingStockholderDao;

    @Override
    public boolean doFilter(String stockCode) {
        //获取公司的季度前十大流通股东信息
        List<FloatingStockholder> floatingStockholderList = floatingStockholderDao.findByStockCode(stockCode);
        //默认获取最近公布的季度信息
        FloatingStockholder floatingStockholder = floatingStockholderList.stream().sorted((o1,o2) -> {
            String jzrq1 = o1.getJzrq();
            String jzrq2 = o2.getJzrq();
            return jzrq2.compareTo(jzrq1);
        }).findFirst().orElse(null);

        if(floatingStockholder == null){
            return false;
        }

        List<FloatingStockholderDetails> detailsList = floatingStockholder.getSdgd();
        if(CollUtil.isEmpty(detailsList)){
            return false;
        }
        //最近披露的季度 前十大流通股 至少有2个机构
        long count = detailsList.stream().filter(item -> !item.getGbxz().equals(Constant.NATURAL_PERSON)).count();
        return count >= atLeastExistOrg;
    }

    @Override
    public String getErrorMsg() {
        return "公司最近披露的前十大流通股中机构数量小于" + atLeastExistOrg + "个";
    }
}
